package com.github.niefy.modules.wx.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.github.niefy.common.utils.Constant;
import com.github.niefy.modules.sys.entity.SysMenuEntity;
import com.github.niefy.modules.wx.dao.WxArticleMapper;
import com.github.niefy.modules.wx.dto.PageSizeConstant;
import com.github.niefy.modules.wx.entity.WxArticle;
import com.github.niefy.modules.wx.service.WxPublicationRecordsService;
import com.qiniu.util.Json;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.freepublish.WxMpFreePublishArticles;
import me.chanjar.weixin.mp.bean.freepublish.WxMpFreePublishItem;
import me.chanjar.weixin.mp.bean.freepublish.WxMpFreePublishList;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialCountResult;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialFileBatchGetResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;

@Service
@Slf4j
public class WxPublicationRecordsServiceImpl implements WxPublicationRecordsService {

    @Autowired
    private WxMpService wxMpService;

    @Autowired
    private WxArticleMapper wxArticleMapper;


    @Override
    public boolean syncWxArticle(String appid) throws WxErrorException {
        List<WxMpFreePublishArticles> WxMpFreePublishArticlesList = new ArrayList<>();
        wxMpService.switchover(appid);
        WxArticle wxArticle = new WxArticle();
        int count = 0;
        int i = 0;
        final int pageSize = PageSizeConstant.PAGE_SIZE_SMALL;
        do{
            int offset=i* pageSize;
            WxMpFreePublishList wxMpFreePublishList = wxMpService.getFreePublishService().getPublicationRecords(offset,pageSize);
            List<WxMpFreePublishItem> WxMpFreePublishItemList = wxMpFreePublishList.getItems();
            for (WxMpFreePublishItem wxMpFreePublishItem:
                    WxMpFreePublishItemList) {
                for (WxMpFreePublishArticles wxMpFreePublishArticles:
                        wxMpFreePublishItem.getContent().getNewsItem()) {
//                    if(!wxMpFreePublishArticles.getIsDeleted()){
//                        WxMpFreePublishArticlesList.add(wxMpFreePublishArticles);
                    wxArticle.setId(null);
                    wxArticle.setUrl(wxMpFreePublishArticles.getUrl());
                    wxArticle.setAuthor(wxMpFreePublishArticles.getAuthor());
                    wxArticle.setContent(wxMpFreePublishArticles.getContent());
                    wxArticle.setDigest(wxMpFreePublishArticles.getDigest());
                    wxArticle.setIsDeleted(wxMpFreePublishArticles.getIsDeleted());
                    wxArticle.setContentSourceUrl(wxMpFreePublishArticles.getContentSourceUrl());
                    wxArticle.setNeedOpenComment(wxMpFreePublishArticles.getNeedOpenComment());
                    wxArticle.setOnlyFansCanComment(wxMpFreePublishArticles.getOnlyFansCanComment());
                    wxArticle.setShowCoverPic(wxMpFreePublishArticles.getShowCoverPic());
                    wxArticle.setThumbMediaId(wxMpFreePublishArticles.getThumbMediaId());
                    wxArticle.setTitle(wxMpFreePublishArticles.getTitle());
                    wxArticle.setAppid(appid);
                    wxArticle.setState("0");
                        wxArticleMapper.insert(wxArticle);
//                    }
                }
            }
            Map<String, Object> returnMap = new HashMap<>();
            count = wxMpFreePublishList.getTotalCount();
//            returnMap.put("count",wxMpFreePublishList.getTotalCount());
//            returnMap.put("data",WxMpFreePublishArticlesList);
            i++;
        }
        while(i==count/pageSize);

        return true;

    }

    @Override
    public Map<String, Object> getPublish(String appid,int page) throws WxErrorException {
        List<WxMpFreePublishArticles> WxMpFreePublishArticlesList = new ArrayList<>();
        wxMpService.switchover(appid);
        final int pageSize = PageSizeConstant.PAGE_SIZE_SMALL;
        int offset=(page-1)* pageSize;
        WxMpFreePublishList wxMpFreePublishList = wxMpService.getFreePublishService().getPublicationRecords(offset,pageSize);
        List<WxMpFreePublishItem> WxMpFreePublishItemList = wxMpFreePublishList.getItems();
        for (WxMpFreePublishItem wxMpFreePublishItem:
        WxMpFreePublishItemList) {
            for (WxMpFreePublishArticles wxMpFreePublishArticles:
                    wxMpFreePublishItem.getContent().getNewsItem()) {
                if(!wxMpFreePublishArticles.getIsDeleted()){
                    WxMpFreePublishArticlesList.add(wxMpFreePublishArticles);
                }
            }
        }
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("count",wxMpFreePublishList.getTotalCount());
        returnMap.put("data",WxMpFreePublishArticlesList);
        return returnMap;
    }

    @Override
    public String getThumb(String appid,String mediaId) throws WxErrorException {
        wxMpService.switchoverTo(appid);
        int imageCount = wxMpService.getMaterialService().materialCount().getImageCount();
        for (int i = 1; i<=imageCount/20+1;i++){
            WxMpMaterialFileBatchGetResult wxMpMaterialFileBatchGetResult = wxMpService.getMaterialService().materialFileBatchGet("image",i, 20);
            List<WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem> wxMaterialFileBatchGetNewsItemList = wxMpMaterialFileBatchGetResult.getItems();
            for (WxMpMaterialFileBatchGetResult.WxMaterialFileBatchGetNewsItem wxMaterialFileBatchGetNewsItem:
                    wxMaterialFileBatchGetNewsItemList) {
                System.out.println(wxMaterialFileBatchGetNewsItem.getMediaId());
                System.out.println(mediaId);
                System.out.println(wxMaterialFileBatchGetNewsItem.getMediaId()==mediaId);
                if(wxMaterialFileBatchGetNewsItem.getMediaId().equals(mediaId)){
                    return wxMaterialFileBatchGetNewsItem.getUrl();
                }
            }
        }
        return "";
    }


    @Override
    public boolean saveBatch(Collection<SysMenuEntity> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<SysMenuEntity> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<SysMenuEntity> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(SysMenuEntity entity) {
        return false;
    }

    @Override
    public SysMenuEntity getOne(Wrapper<SysMenuEntity> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(Wrapper<SysMenuEntity> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<SysMenuEntity> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public BaseMapper<SysMenuEntity> getBaseMapper() {
        return null;
    }

    @Override
    public Class<SysMenuEntity> getEntityClass() {
        return null;
    }
}
